<?php

class IndexController extends Zend_Controller_Action {

    private $offset = 0;
    private $limit = 20;
    private $sessionLogin;
        
    public function init() {
        /* Initialize action controller here */
        $this->sessionLogin = new Zend_Session_Namespace('User');
        //$this->_helper->layout->setLayout('frontend');
    }
    
    public function indexAction() {
        if (empty($this->sessionLogin->userinfo)) {
            $this->_redirect('/index/guest');
        }
        else {
            $this->_redirect('/index/home');
        }
    }
    
    public function guestAction() {
        if (!empty($this->sessionLogin->userinfo)) {
            $this->_redirect('/index/home');
        }
        $this->view->headTitle('Hoi Dap 24G');

        $fromdate = date('Y-m-d', strtotime(time()) - 7 * 86400);  //7day ago
        $todate = date('Y-m-d');

        $arrInput = array(
            'fromdate'  => $fromdate,
            'todate'    => $todate,
            'offset'    => $this->offset,
            'limit'     => $this->limit
        );
        //Get list question
        $arrResultQuestion = (array) Service_Model::factory('Question', 'getQuestionOutHome', $arrInput, 'frontend');

        //Get category
        $arrCate = array();
        $arrResultCate = Service_Model::factory('QuestionCate', 'getQuestionCate', NULL, 'frontend');
        if (count($arrResultCate) > 0) {
            foreach ($arrResultCate as $val) {
                $arrCate[$val['id']] = $val;
            }
        }

        $this->view->question = $arrResultQuestion;
        $this->view->questioncate = $arrCate;
    }

    /**
     * @desc Activation of user
     */
    public function homeAction() {
        if (empty($this->sessionLogin->userinfo)) {
            $this->_redirect('/index/guest');
        }
        //todo        
        $arrResultCate = Service_Model::factory('Category', 'getCategory', NULL, 'frontend');
        //print_r($arrResultCate);
		//die;
        
		$fromdate = date('Y-m-d', strtotime(time()) - 7 * 86400);  //7day ago
        $todate = date('Y-m-d');
		
		//
		// Element right panel
		//
        
		//Get list question
		$arrInput = array(
            'fromdate'  => $fromdate,
            'todate'    => $todate,
            'offset'    => $this->offset,
            'limit'     => $this->limit
        );        
        $arrResultQuestion = (array) Service_Model::factory('Question', 'getQuestionOutHome', $arrInput, 'frontend');
		
		//Get list Associate Replyer 
		$pUserAssocInput = array(           
            'limit'     => 5
        );
		$arrResultUserAssoc = (array) Service_Model::factory('User', 'getUserHaveManyHonorPoint', $pUserAssocInput, 'frontend');
		
		//Get user special list
		$pUserSpecInput = array(           
            'limit'     => 5
        );
		$arrResultUserSpec = (array) Service_Model::factory('User', 'getUserSpecialist', $pUserSpecInput, 'frontend');
		
		
		
        $this->view->category = $arrResultCate;
		$this->view->userSpec = $arrResultUserSpec;
		$this->view->userAssoc = $arrResultUserAssoc;
		$this->view->question = $arrResultQuestion;        
    }

    
//////////////////////////////////////////////////////////////////////////
//                              PRIVATE                                 //
//////////////////////////////////////////////////////////////////////////
    private function __cutString($string, $length) {


        return $str;
    }
}

